package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.queryParams.HisPrQueryParams;
import com.css.fxfzypg.modules.baseData.entity.HisPrEntity;
import com.css.fxfzypg.modules.baseData.service.HisprService;
import com.css.fxfzypg.modules.baseData.vo.HisPrVO;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-22 9:48
 * 历史预评估数据子模块
 */
@RestController
@RequestMapping("/baseData/hisPr")
@Slf4j
public class HisPrController {


    @Resource
    private HisprService hisprService;

    @PostMapping("/saveHisPr")
    public RestResponse saveHisPr(HisPrEntity hisPrEntity){
             RestResponse restResponse = null;
        try {
            boolean res = hisprService.saveHisPr(hisPrEntity);
            restResponse = restResponse.succeed(res);
        } catch (Exception e) {
            log.error("添加历史预评估数据失败",e);
            restResponse =restResponse.fail("添加历史预评估数据失败");
        }
        return restResponse;
    }

    /**
     * 查询详情页
     * @param id
     * @return
     */
    @GetMapping("/getHisPrById/{id}")
    public RestResponse getHisPrById(@PathVariable("id")String id){
        RestResponse restResponse = null;
        try {
            Map<String, Object> map = hisprService.getHisPrById(id);
            restResponse= restResponse.succeed(map);
            restResponse.setMessage("查询历史预评估数据成功");
        } catch (Exception e) {
            log.error("查询历史预评估数据失败",e);
            restResponse=   restResponse.fail("查询历史预评估数据失败");
        }
        return restResponse;
    }

    /**
     * 修改
     * @param hisPrEntity
     * @return
     */
    @PostMapping("/updateHisPr")
    public RestResponse updateHisPr(@RequestBody HisPrEntity hisPrEntity){
        RestResponse restResponse=null;
        try {
            boolean res = hisprService.updateHisPr(hisPrEntity);
            restResponse=RestResponse.succeed(res);
            restResponse.setMessage("修改历史预评估数据成功");
        } catch (Exception e) {
            log.error("修改历史预评估数据失败",e);
            restResponse=restResponse.fail("修改历史预评估数据失败");
        }
        return restResponse;
    }

    /**
     * 分页查询
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllHisPr")
    public RestResponse getAllHisPr(HttpServletRequest request, HisPrQueryParams queryParams){
        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(curPage);
            queryParams.setPageSize(pageSize);
            Map<String,Object> map = hisprService.getAllHisPrByParams(queryParams);
            restResponse = RestResponse.succeed(map);
            restResponse.setMessage("查询历史预评估数据成功!");
        }catch (Exception e){
            log.error("查询历史预评估数据失败!",e);
            restResponse = RestResponse.fail("查询历史预评估数据失败");
        }
        return restResponse;
    }

    /**
     * 根据id删除
     * @param ids
     * @return
     */
    @PostMapping("/deleteHisPr")
    public RestResponse deleteHisPr(@RequestBody List<String> ids){
        RestResponse restResponse = null;
        try {
            boolean res = hisprService.deleteHisPr(ids);
            restResponse = RestResponse.succeed(res);
            restResponse.setMessage("删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            RestResponse.fail("删除失败");
        }
        return restResponse;
    }

    /**
     * excel导出
     * @param ids
     * @throws Exception
     */
    @PostMapping("/exportExcel1")
    public void exportExcel1(HttpServletResponse response, @RequestBody List<String> ids) {
        try {
            hisprService.exportExcel1(response,ids);
            RestResponse.succeed("导成出功");
        } catch (IOException e) {
            log.error("导出失败!",e);
            RestResponse.fail("导出失败!");
        }
    }
    /**
     * excel导出
     * @param queryParams
     * @throws Exception
     */
    @PostMapping("/exportExcel2")
    public void exportExcel2(HttpServletResponse response, HisPrQueryParams queryParams) {
        try {
            hisprService.exportExcel2(response,queryParams);
            RestResponse.succeed("导成出功");
        } catch (IOException e) {
            log.error("导出失败!",e);
            RestResponse.fail("导出失败!");
        }
    }

    /**
     * 导入excel
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/importExcel")
    public RestResponse importExcel(@RequestParam("file") MultipartFile file)throws IOException {
        RestResponse response = null;
        try {
            Map<String, Object> map = hisprService.importExcel(file);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if(res){
                response = RestResponse.succeed("导入成功!");
                response.setMessage("导入成功!");
            }else {
                response = RestResponse.succeed("导入失败!");
                response.setMessage(map.get("mess").toString());
            }
        } catch (IOException e) {
            log.error("导入失败!",e);
            response = RestResponse.fail("导入失败!");
        }
        return response;
    }

    /**
     *获取当前登录人的角色
     * @param
     * @throws IOException
     */
    @GetMapping("/getRole")
    public RestResponse getRole(){
        RestResponse restResponse = null;
        try {
            boolean res = hisprService.getRole();
            restResponse = RestResponse.succeed(res);
            restResponse.setMessage("获取当前登录人角色成功");
        } catch (Exception e) {
            log.error("获取当前登录人角色失败!",e);
            restResponse = RestResponse.fail("获取当前登录人角色失败");
        }
        return restResponse;
    }
}
